<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="报修人" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="报修人"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" prop="contact">
      <el-input v-model="dataForm.contact" placeholder="联系方式"></el-input>
    </el-form-item>
    <el-form-item label="执行方式" prop="taskMethod">
      <el-input v-model="dataForm.taskMethod" placeholder="执行方式"></el-input>
    </el-form-item>
    <el-form-item label="执行时间" prop="taskTime">
      <el-input v-model="dataForm.taskTime" placeholder="执行时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="descp">
      <el-input v-model="dataForm.descp" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="任务类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="任务类型"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="city">
      <el-input v-model="dataForm.city" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="district">
      <el-input v-model="dataForm.district" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="乡镇" prop="township">
      <el-input v-model="dataForm.township" placeholder="乡镇"></el-input>
    </el-form-item>
    <el-form-item label="村" prop="village">
      <el-input v-model="dataForm.village" placeholder="村"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          creator: '',
          contact: '',
          taskMethod: '',
          taskTime: '',
          status: '',
          descp: '',
          createTime: '',
          type: '',
          city: '',
          district: '',
          township: '',
          village: ''
        },
        dataRule: {
          creator: [
            { required: true, message: '报修人不能为空', trigger: 'blur' }
          ],
          contact: [
            { required: true, message: '联系方式不能为空', trigger: 'blur' }
          ],
          taskMethod: [
            { required: true, message: '执行方式不能为空', trigger: 'blur' }
          ],
          taskTime: [
            { required: true, message: '执行时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          descp: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '任务类型不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          district: [
            { required: true, message: '区不能为空', trigger: 'blur' }
          ],
          township: [
            { required: true, message: '乡镇不能为空', trigger: 'blur' }
          ],
          village: [
            { required: true, message: '村不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/taskhis/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.creator = data.taskHis.creator
                this.dataForm.contact = data.taskHis.contact
                this.dataForm.taskMethod = data.taskHis.taskMethod
                this.dataForm.taskTime = data.taskHis.taskTime
                this.dataForm.status = data.taskHis.status
                this.dataForm.descp = data.taskHis.descp
                this.dataForm.createTime = data.taskHis.createTime
                this.dataForm.type = data.taskHis.type
                this.dataForm.city = data.taskHis.city
                this.dataForm.district = data.taskHis.district
                this.dataForm.township = data.taskHis.township
                this.dataForm.village = data.taskHis.village
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/taskhis/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'creator': this.dataForm.creator,
                'contact': this.dataForm.contact,
                'taskMethod': this.dataForm.taskMethod,
                'taskTime': this.dataForm.taskTime,
                'status': this.dataForm.status,
                'descp': this.dataForm.descp,
                'createTime': this.dataForm.createTime,
                'type': this.dataForm.type,
                'city': this.dataForm.city,
                'district': this.dataForm.district,
                'township': this.dataForm.township,
                'village': this.dataForm.village
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
